package dao

import (
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"time"
)

// User 用户数据表
type User struct {
	Id                int64     `json:"id" gorm:"column:id;comment:自增id"`
	UserName          string    `json:"user_name" gorm:"column:user_name;comment:用户名称"`
	OpenId            string    `json:"open_id" gorm:"column:open_id;comment:小程序openId;unique;index;"`
	HeadImage         string    `json:"head_image" gorm:"column:head_image;comment:用户头像"`
	MessageOpenId     string    `json:"message_open_id" gorm:"column:message_open_id;comment:消息提醒公众号openId"`
	OpenMessageStatus uint8     `json:"open_message_status" gorm:"column:open_message_status;comment:是否开通提醒"`
	Phone             string    `json:"phone" gorm:"column:phone;comment:手机号;index;"`
	DeptName          string    `json:"dept_name" gorm:"column:dept_name;comment:单位"`
	Name              string    `json:"name" gorm:"column:name;comment:姓名"`
	Duties            string    `json:"duties" gorm:"column:duties;comment:职务"`
	RoleIds           string    `json:"role_ids" gorm:"column:role_ids;comment:用户角色id;"`
	Status            uint8     `json:"status" gorm:"column:status;comment:0启用 1停用"`
	IsDelete          uint8     `json:"is_delete" gorm:"column:is_delete;comment:1已删除"`
	UpdatedAt         time.Time `json:"update_at" gorm:"column:update_at;comment:更新时间"`
	CreatedAt         time.Time `json:"create_at" gorm:"column:create_at;comment:创建时间"`
}

func (f *User) TableName() string {
	return "user"
}

// InitData 初始化user表
func (f *User) InitData(tx *gorm.DB) error {
	return tx.AutoMigrate(f)
}

// Save 保存
func (f *User) Save(c *gin.Context, tx *gorm.DB) error {
	if err := tx.WithContext(c).Save(f).Error; err != nil {
		return err
	}
	return nil
}
